package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.dao.impl.UserDaoImpl;
import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.util.DBUtil;

public class UserServiceImpl implements UserService {

    @Override
    public User getUserByIdByPass(String userId, String password) {
        User user = new User();
        //创建dao层对象
        UserDao userDao = new UserDaoImpl();
        //获取数据库连接
        DBUtil.getConnection();
        //调用dao层方法
        try {
            user = userDao.getUserByIdByPass(userId,password);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }

        return user;
    }

    @Override
    public int getUserById(String userId) {
        int result = 0;
        //创建dao层对象
        UserDao userDao = new UserDaoImpl();
        //获取数据库连接
        DBUtil.getConnection();
        //调用dao层方法
        try {
            result = userDao.getUserById(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }

        return result;
    }

    @Override
    public int saveUser(String userId, String password, String userName, String userSex) {
        int result = 0;
        //创建dao层对象
        UserDao userDao = new UserDaoImpl();
        //获取数据库连接
        DBUtil.getConnection();
        //调用dao层方法
        try {
            result = userDao.saveUser(userId,password,userName,userSex);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return result;
    }
}
